/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'activity_paramses/' + id,
                model: {
                    url: _global.url.api + 'activity_params',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-remove:click': 'remove',
                        '.edit_listorder:change': 'setlistorder',
                        '.show-offline:click': 'sethidden',
                        '.show-online:click': 'setshow',
                    },
                    edit: function (e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        if (_this.data.type == 1 || _this.data.type == 8) {
                            _this.data.params = jQuery.parseJSON(_this.data.params);
                        }
                        if (_this.data.type == 9) {
                            _this.data.json_params = jQuery.parseJSON(_this.data.params);
                        }
                        if (_this.data.type == 10 || _this.data.type == 11 || _this.data.type == 6 || _this.data.type == 7 || _this.data.type == 12 || _this.data.type == 13) {
                            _this.data.json_params = _this.data.info = jQuery.parseJSON(_this.data.params);
                        }
                        tpl = _this.getTpl(_this.data.type, _this.data);
                        console.log(_this.data);
                        _this.dialog = dialog({
                            title: '素材修改',
                            content: tpl,
                            width: 600,
                            ok: function () {
                                if (_this.data.type == 1) {
                                    params = [];
                                    params.image = $("input[name='imgs']").val();
                                    if (params.image == '' || params.image == undefined) {
                                        Alert({msg: '请上传图片', type: 'error'});
                                        return false;
                                    }
                                    params.url = $('#url').val();

                                    params.width = $("input[name='width']").val();
                                    params.height = $("input[name='height']").val();

                                    _this.data.params = params;
                                    _this.data.img = params.image;
                                    _this.data.url = params.url;
                                    _this.data.width = params.width;
                                    _this.data.height = params.height;

                                }
                                else if (_this.data.type == 5) {
                                    _this.data.params = $("#params").val();
                                    if (_this.data.params == '' || _this.data.params == undefined) {
                                        Alert({msg: '请选择截止日期', type: 'error'});
                                        return false;
                                    }
                                } else if (_this.data.type == 7) {
                                    var title = new Array();
                                    $("input[name='title']").each(function (e) {
                                        if ($.trim($(this).val()) != '') {
                                            title.push($.trim($(this).val()));
                                        }
                                    });
                                    _this.data.titles = title.join(',');
                                    if (_this.data.titles == '') {
                                        Alert({msg: '请填写菜单名称', type: 'error'});
                                        return false;
                                    }

                                    var urls = new Array();
                                    $("input[name='url']").each(function () {
                                        urls.push($.trim($(this).val()));

                                    });
                                    _this.data.urls = urls.join(",");

                                    for (var i = 0; i < urls.length; i++) {
                                        if (!urls[i]) {
                                            Alert({msg: '请填写链接', type: 'error'});
                                            return false;
                                        }
                                    }
                                    _this.data.activity_id = id;
                                } else if (_this.data.type == 8) {
                                    params = [];
                                    _this.data.key = $("#key").val();
                                    if (_this.data.key == '' || _this.data.key == 0) {
                                        Alert({msg: '请选择活动类型', type: 'error'});
                                        return false;
                                    }
                                    _this.data.cate = $("#cate").val();
                                    if (_this.data.cate == '') {
                                        Alert({msg: '请输入类目id', type: 'error'});
                                        return false;
                                    }
                                    _this.data.white = $("#white").val();
                                    _this.data.black = $("#black").val();
                                } else if (_this.data.type == 10) {
                                    _this.data.activity_id = id;
                                    _this.data.title = $("#title").val();
                                    if (_this.data.title == '') {
                                        Alert({msg: '请填写导航名称', type: 'error'});
                                        return false;
                                    }
                                    _this.data.image = $("input[name='imgs']").val();
                                    if (_this.data.image == '' || _this.data.image == undefined) {
                                        Alert({msg: '请上传图片', type: 'error'});
                                        return false;
                                    }
                                    _this.data.url = $("#url").val();
                                    if (_this.data.url == '' || _this.data.url == undefined) {
                                        Alert({msg: '请填写链接', type: 'error'});
                                        return false;
                                    }
                                    _this.data.goods = $("#goods").val();
                                    if (_this.data.goods == '' || _this.data.goods == undefined) {
                                        Alert({msg: '请填写商品id', type: 'error'});
                                        return false;
                                    }
                                } else if (_this.data.type == 9) {
                                    _this.data.activity_id = id;
                                    var title = new Array();
                                    $("input[name='title']").each(function (e) {
                                        if ($.trim($(this).val()) != '') {
                                            title.push($.trim($(this).val()));
                                        }
                                    });
                                    _this.data.titles = title.join(',');
                                    if (_this.data.titles == '') {
                                        Alert({msg: '请填写分类名称', type: 'error'});
                                        return false;
                                    }
                                    var ids = new Array();
                                    $("input[name='activity_id']").each(function (e) {
                                        if ($.trim($(this).val()) != '') {
                                            ids.push($.trim($(this).val()));
                                        }
                                    });
                                    _this.data.ids = ids.join(',');
                                    if (_this.data.ids == '') {
                                        Alert({msg: '请填写活动id', type: 'error'});
                                        return false;
                                    }
                                } else if (_this.data.type == 10) {
                                    _this.data.activity_id = id;
                                } else if (_this.data.type == 11 || _this.data.type == 6) {
                                    var title = new Array();
                                    $("input[name='title']").each(function (e) {
                                        if ($.trim($(this).val()) != '') {
                                            title.push($.trim($(this).val()));
                                        }
                                    });
                                    _this.data.titles = title.join(',');
                                    if (_this.data.titles == '') {
                                        Alert({msg: '请填写分类名称', type: 'error'});
                                        return false;
                                    }
                                    var imgs = new Array();
                                    $("input[name='imgs']").each(function () {
                                        if ($.trim($(this).val()) != '') {
                                            imgs.push($(this).val());
                                        }
                                    });
                                    _this.data.imgs = imgs.join(",");
                                    if (_this.data.imgs == "") {
                                        Alert({msg: '请上传图片', type: 'error'});
                                        return false;
                                    }
                                    var urls = new Array();
                                    $("input[name='url']").each(function () {
                                        urls.push($.trim($(this).val()));

                                    });
                                    _this.data.urls = urls.join(",");
                                    var width = new Array();
                                    $("input[name='width']").each(function () {
                                        width.push($.trim($(this).val()));

                                    });
                                    for (var i = 0; i < width.length; i++) {
                                        if (!width[i]) {
                                            Alert({msg: '请填写图片的宽度', type: 'error'});
                                            return false;
                                        }
                                    }
                                    _this.data.width = width.join(",");
                                    var height = new Array();
                                    $("input[name='height']").each(function () {
                                        height.push($.trim($(this).val()));

                                    });
                                    for (var i = 0; i < height.length; i++) {
                                        if (!height[i]) {
                                            Alert({msg: '请填写图片的高度', type: 'error'});
                                            return false;
                                        }
                                    }

                                    var sort = new Array();
                                    $("input[name='sort']").each(function () {
                                        sort.push($.trim($(this).val()));
                                    });

                                    _this.data.sort = sort.join(",");
                                    _this.data.height = height.join(",");
                                    if (_this.data.type == 11) {
                                        if (imgs.length < 4) {
                                            Alert({msg: '图片导航的图片数必须不少于4张', type: 'error'});
                                            return false;
                                        }
                                    }

                                    _this.data.space = $('[name=space]:checked').val();

                                    _this.data.num = $('[name=num]').val();
                                    console.log($('[name=num]').val());
                                    if (_this.data.num < 3) {
                                        Alert({msg: '导航图片显示的数量不能低于3张', type: 'error'});
                                        return false;
                                    }
                                    _this.data.activity_id = id;
                                } else if (_this.data.type == 12) {
                                    _this.data.activity_id = id;
                                    //_this.data.type = type;
                                    //_this.data.title = $("#title").val();
                                    _this.data.image = $("input[name='imgs']").val();
                                    if (_this.data.image == '' || _this.data.image == undefined) {
                                        Alert({msg: '请上传图片', type: 'error'});
                                        return false;
                                    }
                                    var starttime = _this.data.starttime = $("#starttime").val();
                                    if (_this.data.starttime == '' || _this.data.starttime == undefined) {
                                        Alert({msg: '请选择开始时间', type: 'error'});
                                        return false;
                                    }
                                    var endtime = _this.data.endtime = $("#endtime").val();
                                    if (_this.data.endtime == '' || _this.data.endtime == undefined) {
                                        Alert({msg: '请选择结束时间', type: 'error'});
                                        return false;
                                    }
                                    //判断开始时间是否大于结束时间。
                                    if (starttime > endtime) {
                                        Alert({msg: '开始时间不能大于结束时间！', type: 'error'});
                                        return false;
                                    }

                                } else if (_this.data.type == 13) {
                                    _this.data.activity_id = id;
                                    _this.data.image = $("input[name='imgs']").val();
                                    if (_this.data.image == '' || _this.data.image == undefined) {
                                        Alert({msg: '请上传图片', type: 'error'});
                                        return false;
                                    }
                                    _this.data.goods = $("input[name='goods']").val();
                                    if (_this.data.goods == '' || _this.data.goods == undefined) {
                                        Alert({msg: '请输入商品id', type: 'error'});
                                        return false;
                                    }
                                    _this.data.width = $("input[name='width']").val();
                                    if (_this.data.width == '' || _this.data.width == undefined) {
                                        Alert({msg: '请输入宽度', type: 'error'});
                                        return false;
                                    }
                                    _this.data.height = $("input[name='height']").val();
                                    if (_this.data.height == '' || _this.data.height == undefined) {
                                        Alert({msg: '请输入高度', type: 'error'});
                                        return false;
                                    }
                                } else {
                                    _this.data.params = $("#params").val();
                                    if (_this.data.params == '' || _this.data.params == undefined) {
                                        Alert({msg: '请填写商品id', type: 'error'});
                                        return false;
                                    }
                                }
                                if (model.put()) {
                                    _this.collection.reload();
                                    return true;
                                }
                                return false;
                            }, cancel: function () {
                            }
                        });
                        _this.dialog.show();
                        if (_this.data.json_params && _this.data.type == 9) {
                            if (this.data.json_params) {
                                $.each(this.data.json_params, function (i, info) {
                                    console.log(info)
                                    var html = template('add-tpl', {info: info, i: i});
                                    $("#main-view").append(html);
                                    _this.i++;
                                });

                            }
                        }
                        if (_this.data.json_params && (_this.data.type == 11 || _this.data.type == 6)) {
                            _this.j = 1;
                            if (this.data.json_params) {
                                $.each(this.data.json_params, function (j, info) {
                                    console.log(info)
                                    var html = template('add_picture-tpl', {info: info, j: j});
                                    $("#main-picture_view").append(html);
                                    _this.j++;
                                });
                            }
                        }
                        if (_this.data.json_params && _this.data.type == 7) {
                            _this.x = 1;
                            if (this.data.json_params) {
                                $.each(this.data.json_params, function (x, info) {
                                    console.log(info)
                                    var html = template('add_text-tpl', {info: info, x: x});
                                    $("#main-text-view").append(html);
                                    _this.x++;
                                });
                            }
                        }
                        $('input,select,textarea').on('change', function (e) {
                            _this.change(e);
                        });
                        $('.long_date').on('click', function () {
                            WdatePicker({
                                el: this.id,
                                dateFmt: 'yyyy-MM-dd HH:mm:ss'
                            });
                        });
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function () {
                                if (model.del()) {
                                    _this.collection.reload();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    setshow: function (e) {
                        e.preventDefault();
                        var id = this.data.id;
                        var model = this;
                        dialog({
                            'content': '确定要继续吗?',
                            ok: function () {
                                if (new Model({
                                        url: _global.url.api + 'activity_params_visiable',
                                        data: {id: id, is_visible: 1, act: 'show'}
                                    }).put()) {
                                    _this.collection.fetch();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    sethidden: function (e) {
                        e.preventDefault();
                        var id = this.data.id;
                        var model = this;
                        dialog({
                            'content': '确定要继续吗?',
                            ok: function () {
                                if (new Model({
                                        url: _global.url.api + 'activity_params_visiable',
                                        data: {id: id, is_visible: 0, act: 'show'}
                                    }).put()) {
                                    _this.collection.fetch();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    setlistorder: function (e) {
                        e.preventDefault();
                        var id = this.data.id;
                        var model = this;
                        var listorder = $('input[name="edit_listorder_' + id + '"]').val();
                        if (new Model({
                                url: _global.url.api + 'activity_params_set',
                                data: {id: id, listorder: listorder, act: 'order'}
                            }).put()) {
                            _this.collection.fetch();
                        }
                        ;
                    }
                },
                callback: function () {
                    loaded();
                    console.log(_this.collection.result.activity_group);
                    _global.activity_group = _this.collection.result.activity_group;
                }
            });
            this.collection.fetch();
        },
        getTpl: function (type, data) {
            if (type == 1) {
                return template('banner-tpl', data);
            } else if (type == 2) {
                return template('one-tpl', data);
            } else if (type == 3) {
                return template('ones-tpl', data);
            } else if (type == 4) {
                return template('two-tpl', data);
            } else if (type == 5) {
                return template('timer-tpl', data);
            }
            else if (type == 6) {
                return template('picturetab-1-tpl', data);
            } else if (type == 7) {
                return template('texttab-tpl', data);
            } else if (type == 8) {
                return template('caidantab-tpl', data);
            } else if (type == 10) {
                return template('maodiantab-tpl', data);

            } else if (type == 9) {
                return template('catetab-tpl', data);
            }
            else if (type == 11) {
                return template('picturetab-2-tpl', data);
            }
            else if (type == 12) {
                data.activity_group = _global.activity_group;
                return template('flashprice-tpl', data);
            }
            else if (type == 13) {
                return template('goods_timelimit-tpl', data);
            }
        },
        event: function () {
            var _this = this;
            //保存
            $('#btn-banner').on('click', function (e) {
                _this.showDialog(e, 1, _this);
            });
            $('#btn-view').on('click', function (e) {
                _this.privew_page(e);
            });
            $('#btn-one').on('click', function (e) {
                _this.showDialog(e, 2, _this);
            });
            $('#btn-ones').on('click', function (e) {
                _this.showDialog(e, 3, _this);
            });
            $('#btn-two').on('click', function (e) {
                _this.showDialog(e, 4, _this);
            });
            $('#btn-timer').on('click', function (e) {
                _this.showDialog(e, 5, _this);
            });
            $("#btn-menu").on('click', function (e) {
                _this.showDialog(e, 6, _this);
            });
            $("#btn-font").on('click', function (e) {
                _this.showDialog(e, 7, _this);
            });
            $("#btn-caidantab").on('click', function (e) {
                _this.showDialog(e, 8, _this);
            });

            $("#btn-maodiantab").on("click", function (e) {
                _this.showDialog(e, 10, _this);
            });
            $("#btn-catetab").on('click', function (e) {
                _this.showDialog(e, 9, _this);
            });
            $("#btn-picturetab").on('click', function (e) {
                _this.showDialog(e, 11, _this);
            });
            $("#btn-flashprice").on('click', function (e) {
                _this.showDialog(e, 12, _this);
            });
            $("#btn-goods_timelimit").on('click', function (e) {
                _this.showDialog(e, 13, _this);
            });
            $("#btn-clear_cache").on("click", function (e) {
                e.preventDefault();
                dialog({
                    'content': '确定清除吗',
                    ok: function () {
                        if (new Model({
                                url: _global.url.api + 'clear_active_cache',
                                data: {id: id}
                            }).put()) {
                            _this.collection.reload();
                        }
                        ;
                    }, cancel: function () {
                    }
                }).show();
            });
            _this.i = 1;
            $('body').on('click', "#add_table", function (e) {
                e.preventDefault();
                _this.i++;
                var html = template('add-tpl', _this);
                $("#main-view").append(html);
                var dom = new Array();
                $("#main-view").children('div').each(function () {
                    dom.push($(this).attr('id'));
                });
                if (dom.length > 9) {
                    $(".add_table").hide();
                    return false;
                }
                $(".delete_table").show();
            });
            $('body').on('click', '.delete_table', function (e) {
                e.preventDefault();
                var item = $(this).attr("attr");
                $("#tile_num" + item).remove();
                var dom = new Array();
                $("#main-view").children('div').each(function () {
                    dom.push($(this).attr('id'));
                });
                if (dom.length < 2) {
                    $(".delete_table").hide();
                    return false;
                }
                $(".add_table").show();
            });

            _this.j = 1;
            $('body').on('click', "#add_picture_table", function (e) {
                e.preventDefault();
                _this.j++;
                var html = template('add_picture-tpl', _this);
                $("#main-picture_view").append(html);
                var dom = new Array();
                $("#main-picture_view").children('div').each(function () {
                    dom.push($(this).attr('id'));
                });
                if (dom.length > 9) {
                    $(".add_picture_table").hide();
                    return false;
                }
                $(".delete_picture_table").show();
            });
            $('body').on('click', '.delete_picture_table', function (e) {
                e.preventDefault();
                var item = $(this).attr("attr");
                $("#tile_num_picture" + item).remove();
                var dom = new Array();
                $("#main-picture_view").children('div').each(function () {
                    dom.push($(this).attr('id'));
                });
                if (dom.length < 2) {
                    $(".delete_picture_table").hide();
                    return false;
                }
                $(".delete_picture_table").show();
            });
            _this.x = 1;
            $('body').on('click', "#add_text_table", function (e) {
                e.preventDefault();
                _this.x++;
                var html = template('add_text-tpl', _this);
                $("#main-text-view").append(html);
                var dom = new Array();
                $("#main-text-view").children('div').each(function () {
                    dom.push($(this).attr('id'));
                });
                if (dom.length > 9) {
                    $(".add_text_table").hide();
                    return false;
                }
                $(".delete_text_table").show();
            });
            $('body').on('click', '.delete_text_table', function (e) {
                e.preventDefault();
                var item = $(this).attr("attr");
                $("#tile_num" + item).remove();
                var dom = new Array();
                $("#main-text-view").children('div').each(function () {
                    dom.push($(this).attr('id'));
                });
                if (dom.length < 2) {
                    $(".delete_text_table").hide();
                    return false;
                }
                $(".delete_text_table").show();
            });
            //上传图片
            $('body').on('click', '#btn-upload', function (e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
            $('body').on('click', '.btn-upload_picture', function (e) {
                e.preventDefault();
                var item = $(this).attr("attr");
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        img.item = item;
                        _this.imgPreviewPicture(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },

        privew_page: function (e) {
            e.preventDefault();
            dialog({
                title: '预览',
                content: template('preview_page-tpl', {id: id}),
                width: 320,
                height: 568,
                ok: function () {

                }, cancel: function () {
                }
            }).show();
        },


        showDialog: function (e, type, _this) {
            _this.data = {};
            e.preventDefault();
            tpl = _this.getTpl(type, {});

            _this.dialog = dialog({
                title: '添加素材',
                content: tpl,
                width: 500,
                ok: function () {
                    if (type == 1) {
                        params = [];
                        params.image = $("input[name='imgs']").val();
                        if (params.image == '' || params.image == undefined) {
                            Alert({msg: '请上传图片', type: 'error'});
                            return false;
                        }
                        params.url = $('#url').val();

                        params.width = $("input[name='width']").val();
                        params.height = $("input[name='height']").val();

                        _this.data.params = params;
                        _this.data.activity_id = id;
                        _this.data.type = type;
                    } else if (type == 5) {
                        _this.data.activity_id = id;
                        _this.data.type = type;
                        _this.data.params = $("#params").val();
                        if (_this.data.params == '' || _this.data.params == undefined) {
                            Alert({msg: '请选择截止日期', type: 'error'});
                            return false;
                        }
                    } else if (type == 7) {
                        var title = new Array();
                        $("input[name='title']").each(function (e) {
                            if ($.trim($(this).val()) != '') {
                                title.push($.trim($(this).val()));
                            }
                        });
                        for (var i = 0; i < title.length; i++) {
                            if (!title[i]) {
                                Alert({msg: '请填写标题', type: 'error'});
                                return false;
                            }
                        }
                        _this.data.titles = title.join(',');
                        if (_this.data.titles == '') {
                            Alert({msg: '请填写导航名称', type: 'error'});
                            return false;
                        }

                        var urls = new Array();
                        $("input[name='url']").each(function () {
                            urls.push($.trim($(this).val()));

                        });
                        for (var i = 0; i < urls.length; i++) {
                            if (!urls[i]) {
                                Alert({msg: '请填写url链接', type: 'error'});
                                return false;
                            }
                        }
                        _this.data.urls = urls.join(",");
                        _this.data.activity_id = id;
                        _this.data.type = type;
                    } else if (type == 8) {
                        params = [];
                        params.key = $("#key").val();
                        if (params.key == '' || params.key == 0) {
                            Alert({msg: '请选择活动类型', type: 'error'});
                            return false;
                        }
                        params.cate = $("#cate").val();
                        if (params.cate == '') {
                            Alert({msg: '请输入类目id', type: 'error'});
                            return false;
                        }
                        params.white = $("#white").val();
                        params.black = $("#black").val();
                        _this.data.activity_id = id;
                        _this.data.type = type;
                        _this.data.params = params;
                    } else if (type == 10) {
                        _this.data.activity_id = id;
                        _this.data.type = type;
                        _this.data.title = $("#title").val();
                        if (_this.data.title == '') {
                            Alert({msg: '请填写导航名称', type: 'error'});
                            return false;
                        }
                        _this.data.image = $("input[name='imgs']").val();
                        if (_this.data.image == '' || _this.data.image == undefined) {
                            Alert({msg: '请上传图片', type: 'error'});
                            return false;
                        }
                        _this.data.url = $("#url").val();
                        if (_this.data.url == '' || _this.data.url == undefined) {
                            Alert({msg: '请填写链接', type: 'error'});
                            return false;
                        }
                        _this.data.goods = $("#goods").val();
                        if (_this.data.goods == '' || _this.data.goods == undefined) {
                            Alert({msg: '请填写商品id', type: 'error'});
                            return false;
                        }
                    } else if (type == 12) {
                        _this.data.activity_id = id;
                        _this.data.type = type;
                        //_this.data.title = $("#title").val();
                        _this.data.image = $("input[name='imgs']").val();
                        if (_this.data.image == '' || _this.data.image == undefined) {
                            Alert({msg: '请上传图片', type: 'error'});
                            return false;
                        }
                        var starttime = _this.data.starttime = $("#starttime").val();
                        if (_this.data.starttime == '' || _this.data.starttime == undefined) {
                            Alert({msg: '请选择开始时间', type: 'error'});
                            return false;
                        }
                        var endtime = _this.data.endtime = $("#endtime").val();
                        if (_this.data.endtime == '' || _this.data.endtime == undefined) {
                            Alert({msg: '请选择结束时间', type: 'error'});
                            return false;
                        }
                        //判断开始时间是否大于结束时间。
                        if (starttime > endtime) {
                            Alert({msg: '开始时间不能大于结束时间！', type: 'error'});
                            return false;
                        }

                    } else if (type == 9) {
                        var title = new Array();
                        $("input[name='title']").each(function (e) {
                            if ($.trim($(this).val()) != '') {
                                title.push($.trim($(this).val()));
                            }
                        });
                        _this.data.titles = title.join(',');
                        if (_this.data.titles == '') {
                            Alert({msg: '请填写分类名称', type: 'error'});
                            return false;
                        }
                        var ids = new Array();
                        $("input[name='activity_id']").each(function (e) {
                            if ($.trim($(this).val()) != '') {
                                ids.push($.trim($(this).val()));
                            }
                        });
                        _this.data.ids = ids.join(',');
                        if (_this.data.ids == '') {
                            Alert({msg: '请填写活动id', type: 'error'});
                            return false;
                        }
                        _this.data.activity_id = id;
                        _this.data.type = type;
                    } else if (type == 11 || type == 6) {
                        var title = new Array();
                        $("input[name='title']").each(function (e) {
                            if ($.trim($(this).val()) != '') {
                                title.push($.trim($(this).val()));
                            }
                        });
                        _this.data.titles = title.join(',');
                        if (_this.data.titles == '') {
                            Alert({msg: '请填写导航名称', type: 'error'});
                            return false;
                        }
                        var imgs = new Array();
                        $("input[name='imgs']").each(function () {
                            if ($.trim($(this).val()) != '') {
                                imgs.push($(this).val());
                            }
                        });
                        var sort = new Array();
                        $("input[name='sort']").each(function () {
                            sort.push($.trim($(this).val()));
                        });

                        _this.data.sort = sort.join(",");
                        _this.data.space = $('[name=space]:checked').val();
                        console.log(sort);

                        _this.data.num = $('[name=num]').val();
                        console.log($('[name=num]').val());
                        if (_this.data.num < 3) {
                            Alert({msg: '导航图片显示的数量不能低于3张', type: 'error'});
                            return false;
                        }

                        _this.data.imgs = imgs.join(",");
                        if (_this.data.imgs == "") {
                            Alert({msg: '请上传图片', type: 'error'});
                            return false;
                        }
                        var urls = new Array();
                        $("input[name='url']").each(function () {
                            urls.push($.trim($(this).val()));

                        });
                        for (var i = 0; i < urls.length; i++) {
                            if (!urls[i]) {
                                Alert({msg: '请填写url链接', type: 'error'});
                                return false;
                            }
                        }
                        _this.data.urls = urls.join(",");
                        var width = new Array();
                        $("input[name='width']").each(function () {
                            width.push($.trim($(this).val()));

                        });
                        for (var i = 0; i < width.length; i++) {
                            if (!width[i]) {
                                Alert({msg: '请填写图片的宽度', type: 'error'});
                                return false;
                            }
                        }
                        _this.data.width = width.join(",");
                        var height = new Array();
                        $("input[name='height']").each(function () {
                            height.push($.trim($(this).val()));

                        });
                        for (var i = 0; i < height.length; i++) {
                            if (!height[i]) {
                                Alert({msg: '请填写图片的高度', type: 'error'});
                                return false;
                            }
                        }
                        _this.data.height = height.join(",");
                        if (type == 11) {
                            if (imgs.length < 4) {
                                Alert({msg: '图片导航的图片数必须不少于4张', type: 'error'});
                                return false;
                            }
                        }
                        _this.data.activity_id = id;
                        _this.data.type = type;
                    } else if (type == 13) {
                        _this.data.activity_id = id;
                        _this.data.type = type;
                        _this.data.image = $("input[name='imgs']").val();
                        if (_this.data.image == '' || _this.data.image == undefined) {
                            Alert({msg: '请上传图片', type: 'error'});
                            return false;
                        }
                        _this.data.goods = $("input[name='goods']").val();
                        if (_this.data.goods == '' || _this.data.goods == undefined) {
                            Alert({msg: '请输入商品id', type: 'error'});
                            return false;
                        }
                        _this.data.width = $("input[name='width']").val();
                        if (_this.data.width == '' || _this.data.width == undefined) {
                            Alert({msg: '请输入宽度', type: 'error'});
                            return false;
                        }
                        _this.data.height = $("input[name='height']").val();
                        if (_this.data.height == '' || _this.data.height == undefined) {
                            Alert({msg: '请输入高度', type: 'error'});
                            return false;
                        }
                    }
                    else {
                        _this.data.activity_id = id;
                        _this.data.type = type;
                        _this.data.params = $("#params").val();
                        if (_this.data.params == '' || _this.data.params == undefined) {
                            Alert({msg: '请填写商品id', type: 'error'});
                            return false;
                        }
                    }

                    if (new Model({
                            url: _global.url.api + 'activity_params',
                            data: _this.data
                        }).post()) {
                        _this.collection.reload();
                        return true;
                    }
                    return false;
                }, cancel: function () {
                }
            });
            _this.dialog.show();
            if (type == 9) {
                var html = template('add-tpl', _this);
                $("#main-view").append(html);
                $(".delete_table").hide();
            }
            if (type == 7) {
                var html = template('add_text-tpl', _this);
                $("#main-text-view").append(html);
                $(".delete_table").hide();
            }
            if (type == 11 || type == 6) {
                var html = template('add_picture-tpl', _this);
                $("#main-picture_view").append(html);
                $(".delete_picture_table").hide();
            }
            $('input,select,textarea').on('change', function (e) {
                _this.change(e);
            });
            $('.long_date').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });
        },

        imgPreview: function (img) {
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view').html(template('img-tpl', img));
            }
        },
        imgPreviewPicture: function (img) {
            console.log('this.data', this.data);
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view' + img.item).html(template('img-tpl', img));
            }
        }
    };

    new index();

});